//
//  GluReportDetailVc.m
//  CareRingApp
//
//  Created  on 2025/2/13.
//  血糖报告详情 
    

#import "GluReportDetailVc.h"
#import <Masonry/Masonry.h>
#import <QMUIKit/QMUIKit.h>
#import "ConfigModel.h"
#import "UIViewController+Custom.h"


@interface GluReportDetailVc ()

@property(strong, nonatomic)UIScrollView *scrollView;
@property(strong, nonatomic)UIView *mainContent;
@property(strong, nonatomic)QMUIButton *topResultBtn; //顶部,异常/正常
@property(strong, nonatomic)UILabel *meaTimeLabel; //测量时间+测量类型

@property(strong, nonatomic)UILabel *riskLbl; //风险文字
@property(strong, nonatomic)UILabel *tipsLabel, *insightLabel; //最底下有灯的文字

@property(strong, nonatomic)QMUIButton *riskDescBtn;
@property(strong, nonatomic)UIImageView *riskImageView; // 中间那个脸

@property(strong, nonatomic)UILabel *riskColorLbl; //底部背景有色的btn

//@property(strong, nonatomic)GluIndicatorView *indicatorView;
@property(strong, nonatomic)UILabel *resultLabel;

@end

@implementation GluReportDetailVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
    [self setImageAndText];
}

-(void)setDbGlucoseObj:(DmdbGlucose *)dbGlucoseObj
{
    _dbGlucoseObj = dbGlucoseObj;
#if DEBUG
//    _dbGlucoseObj.current_level = @0;
//    _dbGlucoseObj.risk_code = @"RISK001";// 测试
#endif
}

-(void)initUI
{
    self.mainContent = [UIView new];
    WEAK_SELF
    [self customNavStyleNormal:@"Blood Glucose Report" BackBlk:^{
        STRONG_SELF
        [strongSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(0);
        make.trailing.equalTo(self.view.mas_trailing).inset(0);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
    }];
    
    [self.scrollView addSubview:self.mainContent];
    
    //
    UILabel *topLbl = [UILabel new];
    topLbl.text = @"Measurement Results";
    topLbl.textAlignment = NSTextAlignmentCenter;
    topLbl.font = [UIFont fontWithName:FONT_ARIAL_BOLD size:20.f];
    [topLbl sizeToFit];
    [self.mainContent addSubview:topLbl];
    [topLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLbl.superview.mas_top).offset(15);
        make.centerX.equalTo(topLbl.superview.mas_centerX);
    }];
    // 正常异常
    [self.mainContent addSubview:self.topResultBtn];
    [self.topResultBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.topResultBtn.superview.mas_centerX);
        make.top.equalTo(topLbl.mas_bottom).offset(15);
        
    }];
    
    UIView *drawCnt = [UIView new]; //画图区域
    [self.mainContent addSubview:drawCnt];
    [drawCnt mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(drawCnt.superview.mas_leading).offset(15);
        make.trailing.equalTo(drawCnt.superview.mas_trailing).inset(15);
        make.height.equalTo(@160);
        make.top.equalTo(self.topResultBtn.mas_bottom).offset(25);
    }];
    
    [drawCnt addSubview:self.resultLabel];
    [self.resultLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.equalTo(self.resultLabel.superview);
        
    }];
    
    
    [self.mainContent addSubview:self.meaTimeLabel];
    [self.meaTimeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.meaTimeLabel.superview.mas_centerX);
        make.leading.equalTo(self.meaTimeLabel.superview.mas_leading).offset(15);
        make.trailing.equalTo(self.meaTimeLabel.superview.mas_trailing).inset(15);
        make.top.equalTo(drawCnt.mas_bottom).offset(20);
    }];
    
    // 风向提示
    [self.mainContent addSubview:self.riskImageView];
    [self.mainContent addSubview:self.riskLbl];
    
    [self.riskLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.riskLbl.superview.mas_centerX);
        make.top.equalTo(self.meaTimeLabel.mas_bottom).offset(20);
    }];
    
    [self.mainContent addSubview:self.riskDescBtn];
    [self.riskDescBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.riskLbl.mas_trailing).offset(5);
        make.centerY.equalTo(self.riskLbl);
        make.width.equalTo(self.riskDescBtn.mas_height);
        make.height.equalTo(@20);
    }];
    
    [self.riskImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.riskImageView.superview.mas_centerX);
        make.top.equalTo(self.riskLbl.mas_bottom).offset(8);

    }];
    
    [self.mainContent addSubview:self.riskColorLbl];
    [self.riskColorLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.riskColorLbl.superview.mas_centerX);
        make.height.equalTo(@50);
        make.top.equalTo(self.riskImageView.mas_bottom).offset(15);
        make.leading.equalTo(self.riskColorLbl.superview.mas_leading).offset(40);
        make.trailing.equalTo(self.riskColorLbl.superview.mas_trailing).inset(40);

    }];
   
    
    UIView *insignCnt = [UIView new];
    UIView *tipsCnt = [UIView new];
    [self.mainContent addSubview:insignCnt];
    [self.mainContent addSubview:tipsCnt];
    
    [insignCnt mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.riskColorLbl);
        make.top.equalTo(self.riskColorLbl.mas_bottom).offset(25);
    }];
    [self layoutinsignCnt:insignCnt];

    
    [tipsCnt mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.riskColorLbl);
        make.top.equalTo(insignCnt.mas_bottom).offset(25);
        
    }];
    [self layouttipsCnt:tipsCnt];

    [self.mainContent mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainContent.superview);
        make.centerX.equalTo(self.mainContent.superview.mas_centerX);
        make.width.equalTo(self.mainContent.superview.mas_width);
        make.bottom.equalTo(tipsCnt.mas_bottom).offset(15);
    }];
    
    [self.mainContent layoutIfNeeded];
    [self.view layoutIfNeeded];
    [self.scrollView layoutIfNeeded];
}

-(void)layoutinsignCnt:(UIView *)cnt
{
    [cnt addSubview:self.insightLabel];
    [self.insightLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.insightLabel.superview).offset(8);
        make.leading.equalTo(self.insightLabel.superview).offset(8);
        make.trailing.equalTo(self.insightLabel.superview).inset(8);
        make.bottom.equalTo(self.insightLabel.superview).inset(15);
    }];
//    cnt.backgroundColor = ITEM_BG_COLOR;
    cnt.layer.cornerRadius = ITEM_CORNOR_RADIUS;
    cnt.clipsToBounds = YES;
    
}

-(void)layouttipsCnt:(UIView *)cnt
{
    [cnt addSubview:self.tipsLabel];
    UIImageView *tipIconView = [UIImageView new];
    tipIconView.contentMode = UIViewContentModeScaleAspectFit;
    tipIconView.image = [UIImage imageNamed:@"glu_tip"];
    [cnt addSubview:tipIconView];

    [tipIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(tipIconView.mas_height);
        make.height.equalTo(@15);
        make.top.equalTo(tipIconView.superview);
        make.trailing.equalTo(tipIconView.superview.mas_leading).inset(5);

    }];
    [self.tipsLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(tipIconView.mas_trailing).offset(5);
        make.leading.top.trailing.bottom.equalTo([tipIconView superview]);
    }];
    
    
}

-(void)setImageAndText
{
    // 根据测量结果,风险安排文字和图片
    self.topResultBtn.selected = !self.dbGlucoseObj.isNormal;
    
    NSString *string_1 = [NSString stringWithFormat:@"%@\r\n", self.dbGlucoseObj.meaTimeTypeString];
    // 测量于
    NSDateFormatter *fmt = [NSDateFormatter new];
    fmt.dateFormat = @"MM/dd/yyyy hh:mm";
    NSString *str_2 = [fmt stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.dbGlucoseObj.end_time.doubleValue]];
    
    //
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10; // 设置行间距为 10 点
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    self.meaTimeLabel.attributedText =  [UIViewController bigSmallAttrString:string_1 FontA:[UIFont fontWithName:FONT_ARIAL_BOLD size:17] TxtColorA:UIColor.whiteColor StrB:str_2 FontB:[UIFont systemFontOfSize:15] TxtColorB:UIColor.lightGrayColor ParagraphStyle:paragraphStyle];
    
    self.riskLbl.text = @"Diabetes Risk";
    
    self.riskImageView.image = [UIImage imageNamed:self.dbGlucoseObj.riskIconName];
    
    self.riskColorLbl.backgroundColor = self.dbGlucoseObj.riskColor;
    
    self.riskColorLbl.text = self.dbGlucoseObj.riskTitle;
    
    self.tipsLabel.text = self.dbGlucoseObj.riskTip;
    
  
    
    // insight
    // riskInsight
    NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
    
    paragraphStyle2.lineSpacing = 5; // 设置行间距为 10 点
//    paragraphStyle2.alignment = NSTextAlignmentCenter;
    
    NSString *wholeInsights = self.dbGlucoseObj.riskInsight;
    if (wholeInsights) {
        NSString *insTitle = [wholeInsights componentsSeparatedByString:@"\r\n"].firstObject;
        NSString *restInsigh = [wholeInsights stringByReplacingOccurrencesOfString:insTitle withString:@""];
        
        self.insightLabel.attributedText =  [UIViewController bigSmallAttrString:insTitle FontA:[UIFont fontWithName:FONT_ARIAL_BOLD size:18] TxtColorA:UIColor.whiteColor StrB:restInsigh FontB:[UIFont systemFontOfSize:15] TxtColorB:UIColor.lightGrayColor ParagraphStyle:paragraphStyle2];
        
    } else {
        [self.insightLabel removeFromSuperview];
        // tip上移
        [self.tipsLabel.superview mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.equalTo(self.riskColorLbl);
            make.top.equalTo(self.riskColorLbl.mas_bottom).offset(20);
        }];
    }
  
    
    [self.mainContent layoutIfNeeded];
    [self.view layoutIfNeeded];
    [self.scrollView layoutIfNeeded];
    
    // "mmol/L"
    NSString *unit = @"mmol/L";
    self.resultLabel.text = [NSString stringWithFormat:@"%@%@ - %@%@", self.dbGlucoseObj.lower_bound,unit, self.dbGlucoseObj.upper_bound, unit ];
}

// 跳转风险说明


-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width, CGRectGetMaxY(self.mainContent.frame));
}

-(UIScrollView *)scrollView
{
    if(!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

-(UILabel *)meaTimeLabel
{
    if (!_meaTimeLabel) {
        _meaTimeLabel = [UILabel new];
        _meaTimeLabel.textAlignment = NSTextAlignmentCenter;
        _meaTimeLabel.font = [UIFont systemFontOfSize:15];
        _meaTimeLabel.textColor = UIColor.lightGrayColor;
        _meaTimeLabel.numberOfLines = 0;
    }
    return _meaTimeLabel;
}

-(QMUIButton *)topResultBtn
{
    if (!_topResultBtn)
    {
        
        UIColor * GLU_AB_NORMAL_COLOR = ([UIColor colorWithRed:203/255.0f green:72/255.0f blue:218/255.0f alpha:1]);
        
        UIColor * GLU_NORMAL_COLOR = ([UIColor colorWithRed:49/255.0f green:118/255.0f blue:255/255.0f alpha:1]);
        
        _topResultBtn = [[QMUIButton alloc]init];
        [_topResultBtn setImagePosition:QMUIButtonImagePositionLeft];
        _topResultBtn.spacingBetweenImageAndTitle = 8.f;
        // 正常
        [_topResultBtn setImage:[UIImage imageNamed:@"glu_icon_normal"] forState:UIControlStateNormal];
        [_topResultBtn setTitle:@"Normal" forState:UIControlStateNormal];
        [_topResultBtn setTitleColor: GLU_NORMAL_COLOR forState:UIControlStateNormal];

        // 异常
        [_topResultBtn setImage:[UIImage imageNamed:@"glu_icon_abnormal"] forState:UIControlStateSelected];
        [_topResultBtn setTitle:@"Abnormal" forState:UIControlStateSelected];
        [_topResultBtn setTitleColor:GLU_AB_NORMAL_COLOR forState:UIControlStateSelected];

    }
    return _topResultBtn;
}

-(UILabel *)riskLbl
{
    if (!_riskLbl) {
        _riskLbl = [UILabel new];
        _riskLbl.textAlignment = NSTextAlignmentCenter;
        _riskLbl.font = [UIFont systemFontOfSize:15];
        _riskLbl.textColor = UIColor.lightGrayColor;
        _riskLbl.numberOfLines = 0;
    }
    return _riskLbl;
}
-(UILabel *)riskColorLbl
{
    if (!_riskColorLbl) {
        _riskColorLbl = [UILabel new];
        _riskColorLbl.textAlignment = NSTextAlignmentCenter;
        _riskColorLbl.font = [UIFont fontWithName:FONT_ARIAL_BOLD size:17];
        _riskColorLbl.textColor = UIColor.whiteColor;
        _riskColorLbl.numberOfLines = 0;
        _riskColorLbl.clipsToBounds = YES;
        _riskColorLbl.layer.cornerRadius = ITEM_CORNOR_RADIUS;
    }
    return _riskColorLbl;
}
-(UIImageView *)riskImageView
{
    if (!_riskImageView)
    {
        _riskImageView = [UIImageView new];
        _riskImageView.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    return _riskImageView;
}

-(QMUIButton *)riskDescBtn
{
    if (!_riskDescBtn) {
        _riskDescBtn = [[QMUIButton alloc]init];
        [_riskDescBtn setBackgroundImage:[UIImage imageNamed:@"bg_arrow"] forState:UIControlStateNormal];
    }
    return _riskDescBtn;
}

-(UILabel *)tipsLabel
{
    if (!_tipsLabel) {
        _tipsLabel = [UILabel new];
        _tipsLabel.textAlignment = NSTextAlignmentLeft;
        _tipsLabel.font = [UIFont systemFontOfSize:15];
        _tipsLabel.textColor = UIColor.lightGrayColor;
        _tipsLabel.numberOfLines = 0;
    }
    return _tipsLabel;
}
-(UILabel *)insightLabel
{
    if (!_insightLabel) {
        _insightLabel = [UILabel new];
        _insightLabel.textAlignment = NSTextAlignmentLeft;
        _insightLabel.font = [UIFont systemFontOfSize:15];
//        _insightLabel.textColor = UIColor.lightGrayColor;
        _insightLabel.numberOfLines = 0;
    }
    return _insightLabel;
}



-(UILabel *)resultLabel
{
    if (!_resultLabel) {
        _resultLabel = [UILabel new];
        _resultLabel.textAlignment = NSTextAlignmentCenter;
        _resultLabel.font = [UIFont systemFontOfSize:30];
    }
    return _resultLabel;
}

@end
